package com.suso56.apps.SuSoApp.sph_driver

import android.app.Application
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import androidx.annotation.NonNull
import com.amap.api.location.AMapLocationClient
import com.baidu.mapapi.SDKInitializer
import com.hdgq.locationlib.LocationOpenApi
import com.hdgq.locationlib.entity.ShippingNoteInfo
import com.hdgq.locationlib.listener.OnResultListener
import com.hdgq.locationlib.listener.OnSendResultListener
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.videoplayer.*

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        // 初始化LocationOpenApi
        LocationOpenApi.init(this)
        print("11111")
    }
}
class MainActivity : FlutterActivity() {
    private val CHANNEL = "openLocChannel"
    private lateinit var openApi: LocationOpenApi
    private lateinit var listener: OnResultListener
    private lateinit var sendlistener: OnSendResultListener
    val emptyShippingNoteInfoArray = emptyArray<ShippingNoteInfo>()
    private lateinit var mLocationClient: AMapLocationClient
    @Override
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SDKInitializer.setAgreePrivacy(this.application, true)
        SDKInitializer.initialize(this.application)

//        mLocationClient = new AMapLocationClient(getApplicationContext());

        // 设置隐私合规相关参数
        AMapLocationClient.updatePrivacyAgree(getActivity().getApplicationContext(), true);
        AMapLocationClient.updatePrivacyShow(getActivity().getApplicationContext(), true, true);


        // 初始化 listener 属性
        openApi = LocationOpenApi()
        print("333333")
        listener = object : OnResultListener {
            override fun onFailure(errorCode: String?, errorMessage: String?) {
                // 处理认证失败情况
                println(errorMessage)
            }

            override fun onSuccess(p0: MutableList<ShippingNoteInfo>?) {
                // 处理认证成功情况
                println(p0)
            }
        }
        sendlistener = object : OnSendResultListener {
            override fun onFailure(errorCode: String?, errorMessage: String?,p0: MutableList<ShippingNoteInfo>?) {
                // 处理认证失败情况
                println(errorMessage)
            }

            override fun onSuccess(p0: MutableList<ShippingNoteInfo>?) {
                // 处理认证成功情况
                println(p0)
            }
        }

    }

    // 更新隐私合规，参数值为true表示同意隐私政策
    private fun updatePrivacyAgree(agree: Boolean) {
        // 在这里调用高德地图 SDK 提供的更新隐私合规接口
        // 示例代码，具体方法根据高德地图 SDK 的文档进行调用
        // 例如：mLocationClient.setLocationOption(createLocationOption());
    }

    // 更新隐私合规，参数值为true表示显示隐私政策弹窗
    private fun updatePrivacyShow(show: Boolean) {
        // 在这里调用高德地图 SDK 提供的更新隐私合规接口
        // 示例代码，具体方法根据高德地图 SDK 的文档进行调用
        // 例如：mLocationClient.setLocationOption(createLocationOption());
    }


    override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<out String>, @NonNull grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun configureFlutterEngine(@NonNull flutterEngine:
                                        FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
//        flutterEngine.getPlugins().add(SharedPreferencesPlugin())
//        flutterEngine.getPlugins().add(VideoPlayerPlugin())
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        // 1.创建MethodChannel对象
        val methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)

        // 2.添加调用方法的回调
        methodChannel.setMethodCallHandler {
            // Note: this method is invoked on the main thread.
                call, result ->
            // 2.1.如果调用的方法是getBatteryInfo,那么正常执行
            if (call.method == "startLoc") {
                LocationOpenApi.auth(
                    applicationContext,
                    "com.fossil20.suso56",
                    "5356b879-3631-4941-886b-b17b6ad638ab",
                    "3329701",
                    "debug",
                    listener
                )
                print("44444")
//                print("55555")
//                LocationOpenApi.start(
//                    applicationContext,
//                    "苏E23456",
//                    "王二",
//                    "备注",
//                    arrayOf(
//                        ShippingNoteInfo().apply {
////                            // 假设这里列举的都是 ShippingNoteInfo 的属性，并且它们都有对应的 setter 方法或者是公有属性
//                            shippingNoteNumber = "10001"
//                            serialNumber = "0000"
//                            startCountrySubdivisionCode = "320505"
//                            endCountrySubdivisionCode = "320506"
//                            startLongitude = 31.373998384755588
//                            startLatitude = 120.74717743602213
//                            endLongitude = 31.373998384755588
//                            endLatitude = 120.74717743602213
//                            startLocationText = "苏州市虎丘区"
//                            endLocationText = "苏州市吴中区"
////                            alreadySendCount=0
////                            driverName="王二"
////                            interval=1000
////                            sendCount=0
////                            vehicleNumber="苏E23456"
//
//                        }
//                    ),
//                    listener
//
//                )
                LocationOpenApi.send(
                    applicationContext,
                    "苏E23456",
                    "王二",
                    "备注",
                    arrayOf(
                        ShippingNoteInfo().apply {
                            shippingNoteNumber = call.argument("shippingNoteNumber")
                            serialNumber = call.argument("serialNumber")
                            startCountrySubdivisionCode =
                                call.argument("startCountrySubdivisionCode")
                            endCountrySubdivisionCode = call.argument("endCountrySubdivisionCode")
                            startLongitude = call.argument("startLongitude")
                            startLatitude = call.argument("startLatitude")
                            endLongitude = call.argument("endLongitude")
                            endLatitude = call.argument("endLatitude")
                            startLocationText = call.argument("startLocationText")
                            endLocationText = call.argument("endLocationText")
//                            alreadySendCount=0
//                            driverName="王二"
//                            interval=1000
//                            sendCount=0
//                            vehicleNumber="苏E23456"

                        }
                    ),
                    sendlistener

                )

                // 获取到返回结果
                result.success(1)
            }else if (call.method == "stopLoc"){
                LocationOpenApi.auth(
                    applicationContext,
                    "com.fossil20.suso56",
                    "535bb879-3631-4941-886b-b17b6ad638ab",
                    "3329701",
                    "debug",
                    listener
                )
                LocationOpenApi.stop(
                    applicationContext,
                    "苏E23456",
                    "王二",
                    "备注",
                    arrayOf(
                        ShippingNoteInfo().apply {
                            shippingNoteNumber = call.argument("shippingNoteNumber")
                            serialNumber = call.argument("serialNumber")
                            startCountrySubdivisionCode =
                                call.argument("startCountrySubdivisionCode")
                            endCountrySubdivisionCode = call.argument("endCountrySubdivisionCode")
                            startLongitude = call.argument("startLongitude")
                            startLatitude = call.argument("startLatitude")
                            endLongitude = call.argument("endLongitude")
                            endLatitude = call.argument("endLatitude")
                            startLocationText = call.argument("startLocationText")
                            endLocationText = call.argument("endLocationText")
                        }
                    ),
                    listener

                )
                result.success(1)
            } else {
                // 2.2.如果调用的方法是getBatteryInfo,那么正常执行
                result.notImplemented()
            }
        }
    }


}